<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class  Gosu_payment {
	private $client_id   = 'cack.Rbvyoow386a86jbCCaLLHo35i3rsFR';
	private $service_id  = 'cack.WNksa7aKqziBnKWa34mgb5qUgFG8j7';
	private $service_key = 'cDaPMxcfvS2CRp6tQ2GXfVFNqHHJu3bFdUE8r4LhenrdPt2umdEqbNmC7BTx';
	
  	private $partner_id  = 'cack';
    private $secret_key  = 'ZTvbyGZ2E55gdiPFSSZMdaY5cVhVxvCL9kUpDoT35uexPjWzFXN7BvFmkuKc';
    private $game_id     = 'cack';
	
	function __construct(){		
	}

	public function get_user_info($username){
		$uri = 'http://chanellingpayment.gosu.vn/chanelling/getuserinfo';  
		$signature = md5($this->partner_id . $this->game_id . $username . $this->secret_key); 
		$uri = $uri . '?PartnerID=' . $this->partner_id . '&UserName=' . $username . '&Signature=' . $signature; 
		//echo $uri;exit;
		$result = @file_get_contents($uri); 
		return json_decode($result);
	}
	
	//server = S1, S2
	public function character_exist($username, $server){	
		$uri = 'http://chanellingpayment.gosu.vn/chanelling/CharacterExists';		
		
		$signature = md5($this->partner_id . $this->game_id . $username . $server . $this->secret_key);
		
		$uri = $uri . '?PartnerID=' . $this->partner_id . '&UserName=' . $username . '&Server=' . $server . '&Signature=' . $signature;

		$result = @file_get_contents($uri); 
		  
		return $result;
		
	}
	
	public function recharge($exchange_id, $username, $server, $amount, $time){	
		$uri = 'http://chanellingpayment.gosu.vn/chanelling/Recharge';
		
		$signature = md5($this->partner_id . $this->game_id . $exchange_id . $username . $server . $amount . $time . $this->secret_key);
		
		$uri = $uri . '?PartnerID=' . $this->partner_id . '&ExchangeID=' . $exchange_id . '&UserName=' . $username . '&Server=' . $server . '&Amount=' . $amount . '&Time=' . $time .  '&Signature=' . $signature;
	
		$result = @file_get_contents($uri); 
		
		return $result;
	}
	
	public function init($order_id, $order_info, $amount, $payment_method = 'gosu', $redirect_uri){
		$uri = 'https://id.gosu.vn/paygate/init';
	
		$signature = md5($this->client_id . $this->service_id . $order_id . $amount . $payment_method . $redirect_uri . $this->service_key);
		
		$data = array(
				'Client_id'		=> $this->client_id,
				'Service_id'	=> $this->service_id,
				'Order_id'		=> $order_id,
				'Order_info'	=> $order_info,
				'Amount'		=> $amount,
				'Payment_method'=> $payment_method,
				'Redirect_url'	=> $redirect_uri,
				'Signature'		=> $signature
				);
		
		$result = $this->do_post_request($uri, $data);
		return json_decode($result);
	}
	
	public function query_order($order_id){
		$uri = 'https://id.gosu.vn/paygate/queryorder';
	
		$signature = md5($this->client_id . $this->service_id . $order_id . $this->service_key);
	
		$data = array(
			'client_id'		=> $this->client_id,
			'service_id'	=> $this->service_id,
			'order_id'		=> $order_id,
			'Signature'		=> $signature
		);
	
		$result = $this->do_post_request($uri, $data);
		return json_decode($result);
	}
	
 	private function do_post_request($url, $data) {
        $query = http_build_query($data);

        $ch = curl_init(); // Init cURL
        curl_setopt($ch, CURLOPT_URL, $url); // Post location
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // 1 = Return data, 0 = No return
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // ignore HTTPS
        curl_setopt($ch, CURLOPT_POST, 1); // This is POST
        curl_setopt($ch, CURLOPT_POSTFIELDS, $query); // Add the data to the request

        $response = curl_exec($ch); // Execute the request
        curl_close($ch); // Finish the request
        
        if ($response) {
            return $response;
        } else {
            return NULL;
        }
    }
}